package com.yitao.cms.controller.systemLog;

import com.skipper.base.bean.DataOutput;
import com.skipper.base.bean.PagedOutput;
import com.yitao.client.dto.svclog.YitaoSensitiveWordsDto;
import com.yitao.client.dto.svclog.YitaoSvcLogDto;
import com.yitao.cms.config.aopLog.LogAnnotation;
import com.yitao.cms.service.operatorLog.OperatorLogService;
import com.yitao.cms.service.operatorLog.YitaoSensitiveWordsService;
import com.yitao.cms.service.operatorLog.YitaoSvcLogService;
import com.yitao.cms.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @Auther: wuwenlong
 * @Date: 2018/8/13 0013 09:22
 * @Description:
 */
@Api(value = "systemLog",description = "系统操作日志",tags = {"systemLog"})
@RestController
@RequestMapping(value = "/systemLog",produces = {"application/json"})
public class SystemLogController {
    @Autowired
    private OperatorLogService operatorLogService;
    @Autowired
    private YitaoSvcLogService yitaoSvcLogService;
    @Autowired
    private YitaoSensitiveWordsService yitaoSensitiveWordsService;

    @ApiOperation("审计日志")
//    @LogAnnotation(targetType = "systemLog",remark = "查看日志记录")
    @RequestMapping(value = "/systrmLogFind",method = RequestMethod.POST)
    public PagedOutput systrmLogFind(
            @ApiParam(value = "类型") @RequestParam(value = "targetType" ,required = false) String targetType,
            @ApiParam(value = "操作URL") @RequestParam(value = "operatorURL" ,required = false) String operatorURL,
            @ApiParam(value = "操作人姓名") @RequestParam(value = "operatorName" ,required = false) String operatorName,
            @ApiParam(value = "操作IP") @RequestParam(value = "IP",required = false)String IP,
            @ApiParam(value = "开始时间") @RequestParam(value = "staDate" ,required = false) String staDate,
            @ApiParam(value = "结束时间") @RequestParam(value = "endDate" ,required = false) String endDate,
            @ApiParam(value = "分页参数，表示当前页", required = true, defaultValue = "1") @RequestParam(value = "pageNo", required = true) int  pageNo,
            @ApiParam(value = "分页参数，表示每页显示多少条记录", required = true, defaultValue = "10") @RequestParam(value = "pageSize", required = true) int pageSize
    ){return operatorLogService.findSystermLog(targetType,operatorURL,operatorName,IP,staDate,endDate,pageNo,pageSize);}

    @ApiOperation("访问日志")
    @RequestMapping(value = "/systemSvcLog", method = RequestMethod.POST)
    public PagedOutput<YitaoSvcLogDto> systemSvcLog(@ApiParam(value = "操作人") @RequestParam(value = "nickName", required = false) String nickName,
                                                    @ApiParam(value = "开始时间") @RequestParam(value = "staDate" ,required = false) String staDate,
                                                    @ApiParam(value = "结束时间") @RequestParam(value = "endDate" ,required = false) String endDate,
                                                    @ApiParam(value = "分页参数，表示当前页", required = true, defaultValue = "1") @RequestParam(value = "pageNo", required = true) int  pageNo,
                                                    @ApiParam(value = "分页参数，表示每页显示多少条记录", required = true, defaultValue = "10") @RequestParam(value = "pageSize", required = true) int pageSize){
        Date newStaDate = null;
        Date newEndDate = null;
        if(StringUtils.isNotBlank(staDate) && !"null".equals(staDate) && StringUtils.isNotBlank(endDate) && !"null".equals(endDate)) {
            newStaDate = DateUtils.parseDateMilliFormat(staDate + " 00:00:00");
            newEndDate = DateUtils.parseDateMilliFormat(endDate + " 23:59:59");
        }
        if(StringUtils.isNotBlank(staDate) && !"null".equals(staDate) && (StringUtils.isBlank(endDate) || "null".equals(endDate))) {
            newStaDate = DateUtils.parseDateMilliFormat(staDate + " 00:00:00");
            newEndDate = new Date();
        }
        if(StringUtils.isNotBlank(endDate) && !"null".equals(endDate) && (StringUtils.isBlank(staDate) || "null".equals(staDate))) {
            newStaDate = DateUtils.parseDateMilliFormat("2019-01-01 00:00:01");
            newEndDate = DateUtils.parseDateMilliFormat(endDate + " 23:59:59");
        }
        if(StringUtils.isBlank(nickName)) {
            nickName = null;
        }
        return yitaoSvcLogService.findSystemSvcLog(nickName, newStaDate, newEndDate, pageNo, pageSize);
    }

    @ApiOperation("查询敏感词")
    @RequestMapping(value = "/findSensitiveWords", method = RequestMethod.POST)
    public PagedOutput<YitaoSensitiveWordsDto> findSensitiveWords(@ApiParam(value = "敏感词", required = false) @RequestParam(value = "sensitiveWords") String sensitiveWords,
                                                                  @ApiParam(value = "分页参数，表示当前页", required = true, defaultValue = "1") @RequestParam(value = "pageNo", required = true) int  pageNo,
                                                                  @ApiParam(value = "分页参数，表示每页显示多少条记录", required = true, defaultValue = "10") @RequestParam(value = "pageSize", required = true) int pageSize){
        return yitaoSensitiveWordsService.findSensitiveWords(sensitiveWords, pageNo, pageSize);
    }

    @ApiOperation("新增/编辑敏感词")
    @LogAnnotation(targetType = "addSensitiveWords", remark = "新增/编辑敏感词")
    @RequestMapping(value = "/addSensitiveWords", method = RequestMethod.POST)
    public DataOutput addSensitiveWords(@ApiParam(value = "id") @RequestParam(value = "id", required = false) String id,
                                        @ApiParam(value = "敏感词", required = true) @RequestParam(value = "sensitiveWords") String sensitiveWords,
                                        HttpServletRequest request){
        Long idL = null;
        if(StringUtils.isNotBlank(id) && !"null".equals(id)) {
            idL = Long.valueOf(id);
        }
        return yitaoSensitiveWordsService.addSensitiveWords(idL, sensitiveWords, request);
    }

    @ApiOperation("删除敏感词")
    @LogAnnotation(targetType = "delSensitiveWords", remark = "删除敏感词")
    @RequestMapping(value = "/delSensitiveWords", method = RequestMethod.POST)
    public DataOutput delSensitiveWords(@ApiParam(value = "敏感词id列表", required = true) @RequestParam(value = "ids") List<Long> ids,
                                        HttpServletRequest request){
        return yitaoSensitiveWordsService.delSensitiveWords(ids, request);
    }

    @ApiOperation("导入敏感词-测试接口")
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    public DataOutput importSensitiveWords(@ApiParam(value = "敏感词文本", required = true) @RequestParam(value = "file") MultipartFile file,
                                           HttpServletRequest request){
        return yitaoSensitiveWordsService.importSensitiveWords(file);
    }

}
